#import "PrefsView.h"

@implementation PrefsView

-(id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	app = [ChordsApplication sharedInstance];
	
	CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;

	table = [[UIPreferencesTable alloc] initWithFrame:rect];
	[table setDataSource:self];
	[table setDelegate:self];
	[table reloadData];

	[self addSubview:table];
	
	return self;
}

- (void)reloadData
{
	[table reloadData];
}

#pragma mark ----------------Delegate Methods----------------


- (void)tableRowSelected:(NSNotification *)notification
{
	NSLog(@"selected row %d", [table selectedRow]);
	if ([table selectedRow] == 1)
	{
		NSLog(@"Forzo Notazione");
	}
	if ([table selectedRow] == 3)
	{
		[app showAboutPopUp];
	}
/*	if ([table selectedRow] == 5 or 6)
	{
		[self dosomething]; because a button was pressed
	}
*/
}

#pragma mark ---------------Datasource Methods---------------

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 2;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	switch (group)
	{
		case 0: return 1;
		case 1: return 1;
	}
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	if (group == 0)
	{
		switch (row)
		{
			case 0:
			{
				UIPreferencesControlTableCell *cell = [[UIPreferencesControlTableCell alloc] init];
				[cell setTitle:NSLocalizedStringFromTable(@"PrefsNotation", @"Chords", @"Force notation change")];
				UISwitchControl *switchControl = [[UISwitchControl alloc] initWithFrame:CGRectMake(200., 10., 50., 20.)];
				// [switchControl setAlternateColors:YES];
				[cell setControl:switchControl];
				[switchControl addTarget:self action:@selector(notationControlChange:) forEvents:kUIControlEventMouseUpInside|kUIControlEventMouseUpOutside];
				notationControl=[app getNotationValue];
				[switchControl setValue:notationControl animated:NO];
				[switchControl release];
				return [cell autorelease];
				break;
			}
		}
	}
	else if (group == 1)
	{
		switch (row)
		{
			case 0:
			{
				
				// UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
				UIPreferencesDeleteTableCell *cell = [[UIPreferencesDeleteTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
				[cell setTitle:@"About"];
				[cell setAlignment:2];
				// set the label to white
				CGColorSpaceRef textColor = CGColorSpaceCreateDeviceRGB();
				float white[4] = {1.0, 1.0, 1.0, 1.0};
				[[cell titleTextLabel] setColor:CGColorCreate(textColor, white)];

				return [cell autorelease];
			}
		}
	}
}

- (void)notationControlChange:(UISwitchControl *)control
{
	NSLog([NSString stringWithFormat:@"Switch=%f",[control value]]);
	notationControl=[control value];
}

- (float)getNotationControl {
	return notationControl;
}
/*
- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button
{	
	// NSLog([NSString stringWithFormat:@"initialNotation=%f...notation=%f",initialNotationControl,notationControl]);
	if (button==1)
	{
		if (initialNotationControl!=notationControl)
		{
			[app changeNotation];
			initialNotationControl=notationControl;
		}
		[app showFretBoardView];
	}
}
*/

@end